﻿//初始化用户列表
function initUserList(selector, userGuids, tagManagerOptions) {
    var tagApi = jQuery(selector).tagsManager(tagManagerOptions);

    var users = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/UserAPI/QueryUserByName?nameLike=%QUERY',
            wildcard: '%QUERY',
        }
    });
    jQuery(selector).typeahead(null, {
        name: 'users',
        displayKey: 'name',
        source: users.ttAdapter(),
        templates: {
            empty: [
                '<div class="empty-message">',
                    '无匹配用户',
                '</div>'
            ].join('\n'),
            suggestion: Handlebars.compile('<div><strong>{{name}}</strong> – {{orgName}}</div>')
        }
    }).on('typeahead:selected', function (e, d) {
        tagApi.tagsManager("pushTag", d.name, true, null, d.uid, d.orgName);
    });
    if (userGuids != null) {
        //取得已设置的用户列表
        doOperation({
            'url': '/userapi/GetUserListByGuidList',
            'data': {
                'guidList': userGuids,
            },
            'success': function (d) {
                for (var i in d.Tag) {
                    var user = d.Tag[i];
                    tagApi.tagsManager("pushTag", user.name, true, null, user.uid, user.orgName);
                }
            }
        });
    }
}

//初始化表单列表
function initTempList(selector, formGuid, tempGuid, tagManagerOptions) {
    var tagApi = jQuery(selector).tagsManager(tagManagerOptions);

    var temps = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/FormAPI/QueryUserFormByName?nameLike=%QUERY&tempId=' + tempGuid,
            wildcard: '%QUERY',
        }
    });
    jQuery(selector).typeahead(null, {
        name: 'temps',
        displayKey: 'name',
        source: temps.ttAdapter(),
        templates: {
            empty: [
                '<div class="empty-message">',
                    '无匹配表单',
                '</div>'
            ].join('\n'),
            suggestion: Handlebars.compile('<div><strong>{{name}}</strong> – {{tempName}}</div>')
        }
    }).on('typeahead:selected', function (e, d) {
        tagApi.tagsManager("pushTag", d.name, true, null, d.id, d.template.name);
    });
    if (formGuid != null) {
        //取得已设置的表单
        doOperation({
            'url': '/FormAPI/GetById',
            'data': {
                'formId': formGuid,
            },
            'success': function (d) {
                var form = d.Tag;
                tagApi.tagsManager("pushTag", form.name, true, null, form.id, form.tempName);
            }
        });
    }
}

//初始化表单模板列表
function initFormTemplateList(selector, tempGuid, tagManagerOptions) {
    var tagApi = jQuery(selector).tagsManager(tagManagerOptions);

    var temps = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/FormTemplateAPI/QueryByName?nameLike=%QUERY',
            wildcard: '%QUERY',
        }
    });
    jQuery(selector).typeahead(null, {
        name: 'temp',
        displayKey: 'name',
        source: temps.ttAdapter(),
        templates: {
            empty: [
                '<div class="empty-message">',
                    '无匹配模板',
                '</div>'
            ].join('\n'),
            suggestion: Handlebars.compile('<div><strong>{{name}}</strong></div>')
        }
    }).on('typeahead:selected', function (e, d) {
        tagApi.tagsManager("pushTag", d.name, true, null, d.id, null);
    });
    if (tempGuid != null) {
        //取得已设置的用户列表
        doOperation({
            'url': '/FormTemplateAPI/GetByID',
            'data': {
                'id': tempGuid,
            },
            'success': function (d) {
                tagApi.tagsManager("pushTag", d.Tag.name, true, null, d.Tag.id, null);
            }
        });
    }
}

//初始化组织机构列表
function initOrganizationList(selector, orgGuid, tagManagerOptions) {
    var tagApi = jQuery(selector).tagsManager(tagManagerOptions);

    var temps = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/OrganizeAPI/QueryByName?nameLike=%QUERY',
            wildcard: '%QUERY',
        }
    });
    jQuery(selector).typeahead(null, {
        name: 'temp',
        displayKey: 'name',
        source: temps.ttAdapter(),
        templates: {
            empty: [
                '<div class="empty-message">',
                    '无匹配组织机构',
                '</div>'
            ].join('\n'),
            suggestion: Handlebars.compile('<div><strong>{{name}}</strong></div>')
        }
    }).on('typeahead:selected', function (e, d) {
        tagApi.tagsManager("pushTag", d.name, true, null, d.id, null);
    });
    if (orgGuid != null) {
        //取得已设置的用户列表
        doOperation({
            'url': '/OrganizeAPI/GetByID',
            'data': {
                'orgId': orgGuid,
            },
            'success': function (d) {
                tagApi.tagsManager("pushTag", d.Tag.name, true, null, d.Tag.id, null);
            }
        });
    }
}
